package com.sojson.constant;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.sojson.entity.po.HotelOrder;
import com.sojson.entity.po.HotelOrderRoom;
import com.sojson.entity.po.MessageSystem;
import com.sojson.entity.po.Order;
import com.sojson.entity.po.OrderDeposit;
import com.sojson.entity.po.OrderGoods;
import com.sojson.entity.po.Post;
import com.sojson.enums.EOrderDepositDeductType;
import com.sojson.enums.EPostType;
import com.sojson.util.DateUtil;

/**
 * 系统消息模板类
 * 
 * @author liu
 * @date 2020-11-02
 */
public class ConstantMessageSystemTemplate {

    /** 兼职 */
    public static final String BYWORK = "兼职%s";
    /** 全职 */
    public static final String WORK = "全职%s";
    /** 申请 */
    public static final String APPLY_FOR = "申请";
    /** 取消申请 */
    public static final String APPLY_FOR_CANCEL = "取消申请";
    /** 已通过 */
    public static final String PASSED = "已通过";
    /** 一被拒绝 */
    public static final String REFUSE = "一被拒绝";

    /** 岗位应聘消息 */
    private static final String POST_APPLY = "%s%s{%s}";
    /** 岗位应聘取消消息 */
    private static final String POST_APPLY_CANCEL = "%s%s{%s}";
    /** 应聘岗位审核消息 */
    private static final String POST_APPLY_AUDIT = "%s申请%s";

    /** 诚意金扣除消息标题 */
    public static final String ORDER_DEPOSIT_DEDUCT_TITLE = "诚意金扣除";
    /** 诚意金退款消息标题 */
    public static final String ORDER_DEPOSIT_CANCEL_TITLE = "诚意金退款到微信";
    /** 诚意金提现至余额消息标题 */
    public static final String ORDER_DEPOSIT_TO_BROKERAGE_TITLE = "诚意金提现至余额";
    /** 诚意金扣除消息内容 */
    public static final String POST_APPLY_DEDUCT_CONTENT = "%s-%s%s,%s";
    /** 诚意金退款消息内容 */
    public static final String POST_APPLY_CANCEL_CONTENT = "到账金额：￥%s，到账时间：%s";
    /** 未到岗 */
    public static final String POSTS_NO = "未到岗";
    /** 已取消 */
    public static final String CANCEL = "取消";
    /** 扣除全部诚意金 */
    public static final String DEDUCT_ALL = "扣除全部诚意金";
    /** 扣除一半诚意金 */
    public static final String DEDUCT_HALF = "扣除￥%s诚意金,剩余￥%s已退回微信";

    /** 佣金到账消息标题 */
    public static final String BALANCE_ADD = "佣金到账";
    /** 余额到账消息标题 */
    public static final String BROKERAGE_ADD = "余额到账";
    /** 佣金申请提现消息标题 */
    public static final String BALANCE_APPLY_WITHDRAW_DEPOSIT = "佣金申请提现";
    /** 余额申请提现消息标题 */
    public static final String BROKERAGE_APPLY_WITHDRAW_DEPOSIT = "余额申请提现";
    /** 佣金申请提现审核不通过消息标题 */
    public static final String BALANCE_APPLY_WITHDRAW_DEPOSIT_AUDIT_FAIL = "佣金申请提现审核不通过";
    /** 余额申请提现审核不通过消息标题 */
    public static final String BROKERAGE_APPLY_WITHDRAW_DEPOSIT_AUDIT_FAIL = "余额申请提现审核不通过";
    /** 佣金到账消息标题 */
    public static final String BALANCE_ADD_CONTENT = "到账金额：￥%s，到账时间：%s";
    /** 佣金申请提现消息标题 */
    public static final String BALANCE_APPLY_WITHDRAW_DEPOSIT_CONTENT = "佣金申请提现：￥%s";
    /** 余额申请提现消息标题 */
    public static final String BROKERAGE_APPLY_WITHDRAW_DEPOSIT_CONTENT = "余额申请提现：￥%s";

    /** 订单下单消息标题 */
    public static final String ORDER_ORDERS = "下单通知";
    /** 订单发货消息标题 */
    public static final String ORDER_SEND = "订单发货";
    /** 订单收货消息标题 */
    public static final String ORDER_SIGN_FOR = "订单收货";
    /** 订单退款消息标题 */
    public static final String ORDER_REFUND = "订单退款";
    /** 订单评论消息标题 */
    public static final String ORDER_COMMENT = "评论通知";
    /** 订单消息内容 */
    public static final String ORDER_CONTENT = "订单编号：%s，订单详情：%s";

    /**
     * 获取岗位应聘消息
     * 
     * @param post      岗位信息
     * @param userName  用户姓名
     * @return
     */
    public static MessageSystem getPostApply(Post post, String userName) {
        String applyFor = null;
        Integer type = post.getType();
        if (EPostType.FULL_TIME.getCode() == type) {
            applyFor = WORK;
        } else if (EPostType.SHORT.getCode() == type || EPostType.LONG.getCode() == type) {
            applyFor = BYWORK;
        }
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setContentTypeTwo(type);
        messageSystem.setTitle(String.format(applyFor, APPLY_FOR));
        messageSystem.setContent(String.format(POST_APPLY, userName, APPLY_FOR, post.getName()));
        return messageSystem;
    }

    /**
     * 获取岗位取消应聘消息
     * 
     * @param post      岗位信息
     * @param userName  用户姓名
     * @return
     */
    public static MessageSystem getPostApplyCancel(Post post, String userName) {
        String applyFor = null;
        Integer type = post.getType();
        if (EPostType.FULL_TIME.getCode() == type) {
            applyFor = WORK;
        } else if (EPostType.SHORT.getCode() == type || EPostType.LONG.getCode() == type) {
            applyFor = BYWORK;
        }
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(String.format(applyFor, APPLY_FOR_CANCEL));
        messageSystem.setContent(String.format(POST_APPLY_CANCEL, userName, APPLY_FOR_CANCEL, post.getName()));
        return messageSystem;
    }

    /**
     * 获取应聘岗位审核消息
     * 
     * @param post      岗位信息
     * @param pass      是否录取
     * @return
     */
    public static MessageSystem getPostApplyAudit(Post post, boolean pass) {
        String applyFor = null;
        String passMsg = null;
        Integer type = post.getType();
        if (EPostType.FULL_TIME.getCode() == type) {
            applyFor = WORK;
        } else if (EPostType.SHORT.getCode() == type || EPostType.LONG.getCode() == type) {
            applyFor = BYWORK;
        }
        if (pass) {
            passMsg = PASSED;
        } else {
            passMsg = REFUSE;
        }
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(String.format(applyFor, APPLY_FOR));
        messageSystem.setContent(String.format(POST_APPLY_AUDIT, post.getName(), passMsg));
        return messageSystem;
    }

    /**
     * 获取应聘岗位未到岗消息
     * 
     * @param post      岗位信息
     * @return
     */
    @SuppressWarnings("deprecation")
    public static MessageSystem getPostApplyPostsNo(Post post) {
        Date nowDate = DateUtil.nowDate();
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(ORDER_DEPOSIT_DEDUCT_TITLE);
        messageSystem.setContent(String.format(POST_APPLY_DEDUCT_CONTENT, POSTS_NO,
            nowDate.getMonth() + 1 + "." + nowDate.getDate(), post.getName(), DEDUCT_ALL));
        return messageSystem;
    }

    /**
     * 佣金到账消息
     * 
     * @param total 金额
     * @param date  到账日期
     * @return
     */
    public static MessageSystem getBalanceAccount(BigDecimal total, Date date) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(BALANCE_ADD);
        messageSystem
            .setContent(String.format(BALANCE_ADD_CONTENT, total, DateUtil.dateToString(date, "yyyy-MM-dd HH:mm")));
        return messageSystem;
    }

    /**
     * 余额到账消息
     * 
     * @param total 金额
     * @param date  到账日期
     * @return
     */
    public static MessageSystem getBrokerageAccount(BigDecimal total, Date date) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(BROKERAGE_ADD);
        messageSystem
            .setContent(String.format(BALANCE_ADD_CONTENT, total, DateUtil.dateToString(date, "yyyy-MM-dd HH:mm")));
        return messageSystem;
    }

    /**
     * 佣金提现消息
     * 
     * @param total 金额
     * @return
     */
    public static MessageSystem getBalanceApplyWithdrawDeposit(BigDecimal total) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(BALANCE_APPLY_WITHDRAW_DEPOSIT);
        messageSystem.setContent(String.format(BALANCE_APPLY_WITHDRAW_DEPOSIT_CONTENT, total));
        return messageSystem;
    }

    /**
     * 余额提现消息
     * 
     * @param total 金额
     * @return
     */
    public static MessageSystem getBrokerageApplyWithdrawDeposit(BigDecimal total) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(BROKERAGE_APPLY_WITHDRAW_DEPOSIT);
        messageSystem.setContent(String.format(BROKERAGE_APPLY_WITHDRAW_DEPOSIT_CONTENT, total));
        return messageSystem;
    }

    /**
     * 佣金提现审核不通过消息
     * 
     * @param total 金额
     * @return
     */
    public static MessageSystem getBalanceApplyWithdrawDepositAuditFail(BigDecimal total) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(BALANCE_APPLY_WITHDRAW_DEPOSIT_AUDIT_FAIL);
        messageSystem.setContent(String.format(BALANCE_APPLY_WITHDRAW_DEPOSIT_CONTENT, total));
        return messageSystem;
    }

    /**
     * 余额提现审核不通过消息
     * 
     * @param total 金额
     * @return
     */
    public static MessageSystem getBrokerageApplyWithdrawDepositAuditFail(BigDecimal total) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(BROKERAGE_APPLY_WITHDRAW_DEPOSIT_AUDIT_FAIL);
        messageSystem.setContent(String.format(BROKERAGE_APPLY_WITHDRAW_DEPOSIT_CONTENT, total));
        return messageSystem;
    }

    /**
     * 诚意金退款到微信消息
     * 
     * @param total 金额
     * @param date  到账日期
     * @return
     */
    public static MessageSystem getOrderDepositCancel(BigDecimal total, Date date) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(ORDER_DEPOSIT_CANCEL_TITLE);
        messageSystem.setContent(
            String.format(POST_APPLY_CANCEL_CONTENT, total, DateUtil.dateToString(date, "yyyy-MM-dd HH:mm")));
        return messageSystem;
    }

    /**
     * 获取取消应聘岗位消息
     * 
     * @param post      岗位信息
     * @return
     */
    @SuppressWarnings("deprecation")
    public static MessageSystem getPostApplyCancel(Post post, OrderDeposit orderDeposit) {
        Date nowDate = DateUtil.nowDate();
        BigDecimal payTotal = orderDeposit.getPayTotal();
        BigDecimal refundTotal = orderDeposit.getRefundTotal();
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(ORDER_DEPOSIT_DEDUCT_TITLE);
        String msg = null;
        if (orderDeposit.getDeductType() == EOrderDepositDeductType.ALL.getCode()) {
            msg = DEDUCT_ALL;
        } else {
            msg = String.format(DEDUCT_HALF, payTotal.subtract(refundTotal), refundTotal);
        }
        messageSystem.setContent(String.format(POST_APPLY_DEDUCT_CONTENT, CANCEL,
            nowDate.getMonth() + 1 + "." + nowDate.getDate(), post.getName(), msg));
        return messageSystem;
    }

    /**
     * 获取诚意金提现至余额消息
     * 
     * @return
     */
    public static MessageSystem getOrderDepositToBrokerage(BigDecimal total, Date date) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(ORDER_DEPOSIT_TO_BROKERAGE_TITLE);
        messageSystem.setContent(
            String.format(POST_APPLY_CANCEL_CONTENT, total, DateUtil.dateToString(date, "yyyy-MM-dd HH:mm")));
        return messageSystem;
    }

    /**
     * 获取订单消息
     * 
     * @return
     */
    public static MessageSystem getOrder(Order order, List<OrderGoods> orderGoodss, String title) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(title);

        StringBuilder stringBuilder = new StringBuilder();
        for (OrderGoods orderGoods : orderGoodss) {
            stringBuilder.append(orderGoods.getGoodName()).append("-").append(orderGoods.getSpecsName()).append(",");
        }

        messageSystem.setContent(
            String.format(ORDER_CONTENT, order.getNo(), stringBuilder.substring(0, stringBuilder.length() - 1)));
        return messageSystem;
    }

    /**
     * 获取订单消息
     * 
     * @return
     */
    public static MessageSystem getOrder(HotelOrder order, HotelOrderRoom hotelOrderRoom, String title) {
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setTitle(title);
        messageSystem.setContent(String.format(ORDER_CONTENT, order.getNo(),
            hotelOrderRoom.getNameHotel() + "-" + hotelOrderRoom.getNameRoom()));
        return messageSystem;
    }

}